package com.oracle.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oracle.Until.DateFormatUtil;
import com.oracle.pojo.Role;
import com.oracle.pojo.User;
import com.oracle.service.RoleService;
import com.oracle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class LoginAndLogoutController {


    @Autowired
    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/")
    public String login1(){
        System.out.println("toLogin=========");
        return "login";
    }


@PostMapping("/aa")
public String login( String userCode, String userPassword,  HttpSession session,   Model model){
    User loginUser = userService.login(userCode, userPassword);
    if(loginUser!=null) {
        session.setAttribute("loginUser", loginUser);
        return "index";
    }else{
        model.addAttribute("error", "用户名或密码错误！");
        return "login";}
}
 @GetMapping("/exit")
 public String logout(HttpSession session){
        System.out.println("logout=========");
//        session.invalidate();//session过期 5 return "login"; 6 }
     session.removeAttribute("loginUser");
     return "login";
}
    @Autowired
    private RoleService roleService;
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
    @RequestMapping("/userList")
    public String userList(@RequestParam(value = "keyName",required = false)String keyName,
                           @RequestParam(value = "rid",required = false) Integer rid,
                           Model model,@RequestParam(value = "pageNum",required = false) Integer pageNum){
        System.out.println("你好啊");
        if(keyName==null||keyName==""){keyName="";}else{model.addAttribute("keyName",keyName);}
         if(rid==null||rid==0){rid=0;}else{model.addAttribute("rid",rid);}
        if(pageNum==null||"".equals(pageNum)){pageNum=1;}
        int pageSize = 5;//每页显示数
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userService.getAllUsers(keyName,rid);
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        List<Role> roles = roleService.getAllRoles();

        model.addAttribute("users",pageInfo);
        model.addAttribute("roles",roles);
        return "userList";
    }
    @RequestMapping("/toAddUser")
    public String toAddUser(Model model){
        List<Role> roles = roleService.getAllRoles();
        model.addAttribute("roles",roles);
        return "/userAdd";
    }

    @RequestMapping("/add")
    public String add(String userCode,
                      String userName,
                      String userPassword,
                      int gender,
                      String birthday,
                      String phone,
                      String address,
                      int userRole,
                      HttpSession session) throws Exception {
        User loginUser = (User) session.getAttribute("loginUser");
        User user = new User();
        Date fmtBir = DateFormatUtil.format(birthday);
        user.setUserCode(userCode);
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setGender(gender);
        user.setBirthday(fmtBir);
        user.setPhone(phone);
        user.setAddress(address);
        Role role = new Role();
        role.setId(userRole);
        user.setRole(role);
        user.setCreatedBy(loginUser.getId());
        user.setModifyBy(loginUser.getId());
        user.setCreationDate(new Date());
        user.setModifyDate(new Date());
        if(userService.addUser(user)){
            return "redirect:/userList";
        }
        return "redirect:/toAddUser";

}
    @RequestMapping("/valiUserCode")
    @ResponseBody
    public String valiUserCode(String userCode){
        if(userService.findUserCountByUserCode(userCode)>0){
            return "find";
        }else {
            return "notfind";
        }
    }
    @RequestMapping("/toview/{id}")
    public String toView(@PathVariable int id,Model model){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "userView";
    }
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable int id,Model model){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        List<Role> roles = roleService.getAllRoles();
        model.addAttribute("roles",roles);
        return "userUpdate";
    }
    @RequestMapping("/update")
    public String updateUser(
            int id,
            String userName,
            int gender,
            String birthday,
            String phone,
            String address,
            int userRole,
            HttpSession session
    ) throws Exception {
        User loginUser = (User) session.getAttribute("loginUser");
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setGender(gender);
        user.setPhone(phone);
        user.setAddress(address);
        Date fmtDate = DateFormatUtil.format(birthday);
        user.setBirthday(fmtDate);
        Role role = new Role();
        role.setId(userRole);
        user.setRole(role);
        user.setModifyDate(new Date());
        user.setModifyBy(loginUser.getId());
        if(userService.updateUser(user)){
            return "redirect:/userList";
        }
        return "redirect:/toupdate/"+user.getId();
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUserById(int id){
    if(userService.deleteUserById(id)){
        return "success";
    }else{
        return "fail";
    }
    }

}